package org.example.account.service.impl;

import org.example.account.mapper.AccountMapper;
import org.example.account.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@Service
public class AccountServiceImpl implements AccountService {
    private static final Logger log = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Resource
    private AccountMapper accountMapper;

    @Override
    public void decrease(Long userId, BigDecimal money) throws InterruptedException {
        log.info("扣减用户余额 --- start");
        accountMapper.decrease(userId, money);
        if (money.compareTo(BigDecimal.valueOf(200)) == 0) {
            // 模拟超时
            TimeUnit.SECONDS.sleep(20);
        }
        log.info("扣减用户余额 --- end");
    }
}
